package StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void startStudentSystem() {
        ArrayList<Student>list=new ArrayList<>();
        //给循环定义名字用来终止外层循环
        loop: while (true) {
            System.out.println("--------------------欢迎来到苏月学生管理系统-----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choice = sc.next();
            switch (choice) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    //break只能终止内层选择
                    //break loop;
                    System.exit(0);//停止虚拟机运行，按×退出窗口也能执行这一代码停止虚拟机
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }
    public static void addStudent(ArrayList<Student> list) {
        System.out.println("添加学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生的id");
        String id = sc.next();
        boolean flag = false;;
        //判断一个容器里面是否有相同的元素，可以采用两循环两if判断结构解决
        //外循环和if用于添加再次循环的内容，内循环和if用于遍历判断元素
        //通过内if来控制外if，并且两个循环都要有出口（break）
        loop:while (true) {
            if(flag){   System.out.println("id重复");
                System.out.println("请重新输入学生的id");
                id = sc.next();
                flag = false;
            }

            for (int i = 0; i < list.size(); i++) {
                Student  student = list.get(i);
                if (student.getId().equals(id)) {
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                break loop;
            }

        }
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        Student stu = new Student(id, name, age, address);
        list.add(stu);
        System.out.println("添加成功");
    }
    public static void deleteStudent(ArrayList<Student>list) {
        System.out.println("删除学生");
        if(list.size()==0){
            System.out.println("当前无学生信息，请添加后再删除");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId().equals(id)) {
                list.remove(i);
                System.out.println("id为"+id+"的学生删除成功");
                return;
            }
        }
        System.out.println("id不存在，删除失败");
    }
    public static void updateStudent(ArrayList<Student>list) {
        System.out.println("修改学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId().equals(id)) {
                System.out.println("请输入要修改学生的姓名");
                String newName = sc.next();
                student.setName(newName);
                System.out.println("请输入要修改学生的年龄");
                int newAge = sc.nextInt();
                student.setAge(newAge);
                System.out.println("请输入要修改学生的家庭住址");
                String newAddress = sc.next();
                student.setAddress(newAddress);
                System.out.println("学生信息修改成功");
                return;
            }
        }
        System.out.println("要修改的id"+id+"不存在，请重新输入");
    }
    public static void queryStudent(ArrayList<Student> list) {
        System.out.println("查询学生");
        if(list.size()==0){
            System.out.println("当前无学生信息，请添加后再查询");
        }
        //用\t打印表头信息
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t\t"+student.getAddress());
        }
    }
}
